﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Karo
{
    static class Drawing
    {

        /// <summary>
        /// drawing size of a tile in pixels
        /// </summary>
        public static Size TileSize;

        //Drawing variables
        private static readonly Color _lineColor    = Color.DarkSlateGray;
        private static readonly int _lineThickness  = 1; 
        private static readonly Color _tileColor    = Color.Khaki;
        private static readonly Color _tileClickedColor = Color.LightPink;
        private static readonly int _pawnLineThickness = 3;

        

        /// <summary>
        /// Draws the gameboard
        /// </summary>
        /// <param name="graphics">We are gonna do some painting on you..</param>
        /// <param name="board">The game board itself with all the information</param>
        /// <param name="boxSize">size of the drawing area</param>
        public static void DrawBoard(Graphics graphics, Board board, Size boxSize)
        {
            //Calculate the size of the tiles
            CalculateTileSize(board, boxSize);

            //Draw the tiles with the pawns on it
            DrawTiles(graphics, board);

            //Draw the lines on the board
            DrawBoardLines(graphics, board);
        }

        /// <summary>
        /// Draws a Pawn on the board
        /// </summary>
        /// <param name="graphics">The graphics we are drawing on</param>
        /// <param name="pawn">The pawn that needs to be drawn on the board</param>
        /// <param name="tileRect">the drawing rectangle of the tile we are going to draw the pawn on</param>
        private static void DrawPawn(Graphics graphics, Pawn pawn, Rectangle tileRect)
        {
            Brush pawnBrush = new SolidBrush(pawn.belongsToPlayer.PlayerColor);
            Pen pawnPen = new Pen(pawn.belongsToPlayer.PlayerColor, _pawnLineThickness);

            if (pawn.IsUpsideDown)
            {
                tileRect.Width--;
                tileRect.Height--;
                graphics.DrawEllipse(pawnPen, tileRect);
            }
            else
            {
                tileRect.X--;
                tileRect.Y--;
                tileRect.Width++;
                tileRect.Height++;
                graphics.FillEllipse(pawnBrush, tileRect);
            }

        }

        /// <summary>
        /// Draw all the tiles that are on the board
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="board"></param>
        private static void DrawTiles(Graphics graphics, Board board)
        {
            Brush tileBrush = new SolidBrush(_tileColor);
            Brush tileBrushClicked = new SolidBrush(_tileClickedColor);

            foreach (Tile tile in board.tileList)
            {
                //Starting point of the rectangle we are going to draw in pixels
                //Dont forget to add the line thickness otherwise we are going to loose our precious line
                Point tileStartingPoint = new Point(tile.Location.X * TileSize.Width + _lineThickness, 
                    tile.Location.Y * TileSize.Height + _lineThickness);
              
                //We are going to draw the tiles inside the box so the size will the TileSize minus the thickness of the lines
                Size tileDrawingSize = new Size(TileSize.Width - _lineThickness, TileSize.Height - _lineThickness);

                //Rectangle we are going to draw
                Rectangle tileRect = new Rectangle(tileStartingPoint, tileDrawingSize);

                if(tile.clicked)
                    graphics.FillRectangle(tileBrushClicked, tileRect);
                else
                    graphics.FillRectangle(tileBrush, tileRect);

                if (tile.pawn != null)
                    DrawPawn(graphics, tile.pawn, tileRect);
            }
        }

        /// <summary>
        /// Draws the lines on the board
        /// </summary>
        /// <param name="graphics">The grapics the lines are drawn on</param>
        /// <param name="board">The game board</param>
        private static void DrawBoardLines(Graphics graphics, Board board)
        {
            Pen drawingPen = new Pen(_lineColor, _lineThickness);
            //Draw the horizontal lines
            for (int i = 0; i < board.BoardSize.Height + 1; i++)
            {
                graphics.DrawLine(drawingPen, new Point(0, i * TileSize.Height),
                                                new Point(board.BoardSize.Width * TileSize.Width, i * TileSize.Height));
            }

            //Draw the vertical lines
            for (int i = 0; i < board.BoardSize.Width + 1; i++)
            {
                graphics.DrawLine(drawingPen, new Point(i * TileSize.Width, 0),
                                                new Point(i * TileSize.Width, board.BoardSize.Height * TileSize.Height));
            }
        }

        /// <summary>
        /// Function that calculates and sets the TileSize 
        /// </summary>
        /// <param name="board">the game board</param>
        /// <param name="boxSize">size of the the area we are drawing on</param>
        private static void CalculateTileSize(Board board, Size boxSize)
        {
            TileSize = new Size();
            TileSize.Width = boxSize.Width / board.BoardSize.Width;
            TileSize.Height = boxSize.Height / board.BoardSize.Height;
        }




    }
}
